package _Pan3D.display3D {
	import _Pan3D.display3D.interfaces.IDisplay3DContainer;
	
	import _me.Scene_data;
	
	import flash.display3D.Program3D;
	import flash.events.EventDispatcher;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	/**
	 * @author MSN:liuyan3757@163.com  QQ: 421537900
	 */
	public class Display3D extends EventDispatcher
	{
		protected var _x:Number = 0;
		protected var _y:Number = 0;
		protected var _z:Number = 0;
		protected var _id:uint=0;
		
		protected var _rotationX:Number = 0;
		protected var _rotationY:Number = 0;
		protected var _rotationZ:Number = 0;
		
		protected var _scale:Number = 1;
		
		protected var modelMatrix:Matrix3D;
		protected var posMatrix:Matrix3D;
		private var _parent:IDisplay3DContainer;
	
		
		protected var _absoluteX:Number = 0;
		protected var _absoluteY:Number = 0;
		protected var _absoluteZ:Number = 0;
		
		protected var _visible:Boolean = true;
		
		public var loadComplete:Boolean;
		//public var program:Program3D;
		protected var _program:Program3D;
		
		
		public function Display3D()
		{
			modelMatrix = new Matrix3D;
			posMatrix = new Matrix3D;
			//updataPos();
		}
		
		public function get x():Number
		{
			return _x;
		}

		public function set x(value:Number):void
		{
			_x = value;
			updataPos();
		}

		public function get y():Number
		{
			return _y;
		}

		public function set y(value:Number):void
		{
			_y = value;
			updataPos();
		}

		public function get z():Number
		{
			return _z;
		}

		public function set z(value:Number):void
		{
			_z = value;
			updataPos();
		}

		public function get rotationX():Number
		{
			return _rotationX;
		}

		public function set rotationX(value:Number):void
		{
			_rotationX = value;
		}

		public function get rotationY():Number
		{
			return _rotationY;
		}

		public function set rotationY(value:Number):void
		{
			_rotationY = value;
			updatePosMatrix();
		}

		public function get rotationZ():Number
		{
			return _rotationZ;
		}

		public function set rotationZ(value:Number):void
		{
			_rotationZ = value;
		}

		public function get scale():Number
		{
			return _scale;
		}

		public function set scale(value:Number):void
		{
			_scale = value;
			updatePosMatrix();
		}
		
		public function updateMatrix():void{
			modelMatrix.identity();
			modelMatrix.prepend(Scene_data.cam3D.cameraMatrix);
			modelMatrix.prepend(posMatrix);
		}

		public function get parent():IDisplay3DContainer
		{
			return _parent;
		}

		public function set parent(value:IDisplay3DContainer):void
		{
			_parent = value;
			updataPos();
		}
		
		public function updataPos():void{
			if(this._parent){
				this._absoluteX = this._x + this._parent.absoluteX;
				this._absoluteY = this._y + this._parent.absoluteY;
				this._absoluteZ = this._z + this._parent.absoluteZ;
			}
			updatePosMatrix();
		}
		
		public function updatePosMatrix():void{
			posMatrix.identity();
			posMatrix.prependTranslation(this._absoluteX, this._absoluteY, this._absoluteZ);
			posMatrix.prependScale(this._scale,this._scale,this._scale);
			posMatrix.prependRotation(_rotationY , Vector3D.Y_AXIS);
				
		}

		public function get absoluteX():Number
		{
			return _absoluteX;
		}

		public function set absoluteX(value:Number):void
		{
			_absoluteX = value;
		}

		public function get absoluteY():Number
		{
			return _absoluteY;
		}

		public function set absoluteY(value:Number):void
		{
			_absoluteY = value;
		}

		public function get absoluteZ():Number
		{
			return _absoluteZ;
		}

		public function set absoluteZ(value:Number):void
		{
			_absoluteZ = value;
		}
		
		public function setProgram3D(value:Program3D):void{
			program = value;
		}

		public function get visible():Boolean
		{
			return _visible;
		}

		public function set visible(value:Boolean):void
		{
			_visible = value;
		}

		public function get id():uint
		{
			return _id;
		}

		public function set id(value:uint):void
		{
			_id = value;
		}

		public function get program():Program3D
		{
			return _program;
		}

		public function set program(value:Program3D):void
		{
			_program = value;
		}


	}
}